#ifndef SYS_H
#define SYS_H

#include "Common_Types.h"
#if defined(linux) || defined(__CYGWIN__)
	#include <time.h>
#include <sys/timex.h>
#elif defined(__APPLE__)
	#include <sys/time.h>
#endif

//#define DEBUG_MESSAGE

#define PERROR(x, ...) message(LOG_ERR, x ": %m\n", ##__VA_ARGS__)
#define NOTIFY(x, ...) message(LOG_NOTICE, x, ##__VA_ARGS__)
#define INFO(x, ...)   message(LOG_INFO, x, ##__VA_ARGS__)

#ifdef DEBUG_MESSAGE
	#define DBG(x, ...) message(LOG_DEBUG, x, ##__VA_ARGS__)
#else
	#define DBG(x, ...)
#endif
#include <syslog.h>

typedef enum
{
	REQRT_INTERVAL_TIMER =0,
	IL2CLRT_INTERVAL_TIMER,
	INTERVAL_TIMER_SIZE
}INTERVAL_TIMER_ENUM;
typedef struct {
  U32 seconds;
  U32 nanoseconds;

} TimeInternal;

typedef struct {
  U32  interval;
  U32  left;
  bool expire;
} IntervalTimer;

TimeInternal subtractTime(const TimeInternal* t2, const TimeInternal* t1);
TimeInternal addTime(const TimeInternal* t1, const TimeInternal* t2);
bool greaterThan(const TimeInternal* t1, const TimeInternal* t2);
bool nanoSleep(TimeInternal * t);
void getTime(TimeInternal * time);
void message(int priority, const char *format, ...);

void catch_alarm(int sig);
void initTimer(void);
void timerUpdate(IntervalTimer * itimer);
void timerStop(U16 index, IntervalTimer * itimer);
void timerStart(U16 index, U16 interval, IntervalTimer * itimer);
bool timerExpired(U16 index, IntervalTimer * itimer);

#endif
